package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import com.sc.nft.dao.EquitySharePoolDao;
import com.sc.nft.entity.EquityProfitRule;
import com.sc.nft.entity.EquitySharePanel;
import com.sc.nft.entity.EquitySharePool;
import com.sc.nft.entity.EquitySharePoolDetail;
import com.sc.nft.exception.GlobalRunTimeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.EquitySharePoolService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 服务接口实现
 *
 * @author zxy
 * @since 2024-08-31 18:09:02
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EquitySharePoolServiceImpl implements EquitySharePoolService {
    private final EquitySharePoolDao equitySharePoolDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPool(EquitySharePanel panel, Long orderId, BigDecimal amount) {
        EquitySharePool byId = equitySharePoolDao.getById(panel.getSharePoolId());
        Assert.notNull(byId,()-> new GlobalRunTimeException("分红池不存在"));
        EquitySharePoolDetail detail = new EquitySharePoolDetail();
        detail.setSharePoolId(panel.getSharePoolId());
        detail.setAmount(amount.multiply(panel.getShareRatio()));
        detail.setType(1);
        detail.setLinkId(orderId);
        detail.insert();
        equitySharePoolDao.addAmount(detail.getSharePoolId(),detail.getAmount());
    }

    @Override
    public void addPool(Long poolId, Long orderId, BigDecimal amount) {
        EquitySharePool byId = equitySharePoolDao.getById(poolId);
        Assert.notNull(byId,()-> new GlobalRunTimeException("分红池不存在"));
        EquitySharePoolDetail detail = new EquitySharePoolDetail();
        detail.setSharePoolId(poolId);
        detail.setAmount(amount);
        detail.setType(1);
        detail.setLinkId(orderId);
        detail.insert();
        equitySharePoolDao.addAmount(detail.getSharePoolId(),detail.getAmount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void subPool(EquityProfitRule profitRule,  BigDecimal amount) {
        EquitySharePool byId = equitySharePoolDao.getById(profitRule.getSharePoolId());
        Assert.notNull(byId,()-> new GlobalRunTimeException("分红池不存在"));
        EquitySharePoolDetail detail = new EquitySharePoolDetail();
        detail.setSharePoolId(profitRule.getSharePoolId());
        detail.setAmount(amount);
        detail.setType(2);
        detail.setLinkId(profitRule.getId());
        detail.insert();
        equitySharePoolDao.subAmount(detail.getSharePoolId(),detail.getAmount());
    }
}